define(function (require, exports, module) {
    'use strict';
    var $ = require('jquery'),
        _ = require('underscore'),
        Backbone = require('backbone'),
        notify = require('js/utils/notify'),
        models = require('js/apps/report/models'),
        utils = require('js/utils/tools'),
        chart = require('js/utils/chart'),
        moment = require('moment'),
        helper = require('js/apps/report/common'),
        app = Backbone.history;

    var GameReportView = Backbone.View.extend({
        events: {
            'click #search': 'doSearch'
        },
        initialize: function (options) {
            this.options = options;
            this.model = new models.GameReport();
            this.model.bind('change reset', this.renderWithData, this);
        },
        doSearch: function () {
            var options = {};
            options.dateRange = helper.getDateRange();
            options.date_range = options.dateRange.join(',');
            if($('#searchGameFilter').val() != -1 && $('#searchGameFilter').val() !=0){
                options.game_filter = $('#searchGameFilter').val();
            } else if($('#searchGameFilter').val() ==0) {
                options.by_sum = 1;
            }
            options.by_new_user = $('#ByNewUser').val();
            var searched = options;
            utils.saveSearched('report/game_report/', searched);
            app.navigate(utils.composeQueryString('#report/game_report/', options), {trigger: true});
        },
        render: function () {
            this.$el.html("");
            $(window).scrollTop(0);
            return this;
        },
        renderWithData: function () {
            this.$el.html(this.template({
                items: this.model.list,
                options: this.options,
                searched: utils.getSearched('report/game_report/')
            }));
            helper.loadDatePicker(this.options);
            utils.multiselect('#searchGameFilter', true, {
                'includeSelectAllOption': true
            });
            var that = this;
            utils.renderTable('main-list', {
                $orderby: this.options.$orderby || 'rank',
                sortCallback: function (field) {
                    that.options.$orderby = field;
                    var desc = false;
                    if (field[0] == '-') {
                        desc = true;
                        field = field.substr(1);
                    }
                    that.model.list = _.sortBy(that.model.list, function (o) {
                        if (desc) return 0 - o[field];
                        return o[field];
                    });
                    that.renderWithData();
                }
            });
            return this;
        },
        load: function () {
            this.model.fetch({
                reset: true,
                data: this.options,
                error: function (c, r, o) {
                    notify.notifyResp(r);
                }
            });
        }
    });
    exports.List = Backbone.View.extend({
        el: "#content_wrapper",
        template: "report/GameReport.html",
        initialize: function (options) {
            this.options = options;
        },
        render: function () {
            var today = moment().format('YYYY-MM-DD'),
                dateRange = [today, today];
            if(this.options.dateRange === undefined){
                this.options.dateRange = dateRange;
                this.options.date_range = this.options.dateRange.join(',');
            }else{
                this.options.dateRange = JSON.parse(this.options.dateRange)
            }
            var view = new GameReportView(this.options);
            view.template = this.template;
            this.$el.empty();
            this.$el.append(view.render().el);
            view.load();
            return this;
        }
    });

});
